package com.kkb.wx.contorller;

import com.kkb.controller.BaseController;
import com.kkb.domain.AjaxResult;
import com.kkb.pojo.TWxMember;
import com.kkb.wx.service.CategoryService;
import com.kkb.wx.service.WxMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//題目分类控制器
@Controller
public class CategoryController extends BaseController {
    @Autowired
    private WxMemberService wxMemberService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping("category/list")
    @ResponseBody
    public AjaxResult getCategoryList(@RequestBody HashMap<String,Object> data){
        try{
            //从header中获取OpenID
            String openId = getHeaderAuthorization();
            //根据openId 获取会员，同时获取memberId，cityId,courseId
            TWxMember wxMember = wxMemberService.findByOpenId(openId);
            data.put("memberId",wxMember.getId());
            data.put("courseId",wxMember.getCourseId());
            data.put("cityId",wxMember.getCityId());
            //调用Service 获取题目分类列表
            List<Map> mapList= categoryService.findCategory(data);
            return AjaxResult.success(mapList);
        }catch(Exception e){
            logger.error("获取题目失败"+e.getMessage());
            return AjaxResult.error("操作失败"+e.getMessage());
        }
    }

    //获取某一分类下的分类信息及题目列表
    @PostMapping("/category/questions/list")
    @ResponseBody
    public AjaxResult getCategoryQuestionList(@RequestBody HashMap<String,Object> mapData){
        try{
            //从header中获取OpenID
            String openId = getHeaderAuthorization();
            //根据openId 获取会员，同时获取memberId，cityId,courseId
            TWxMember wxMember = wxMemberService.findByOpenId(openId);
            mapData.put("memberId",wxMember.getId());
            mapData.put("courseId",wxMember.getCourseId());
            mapData.put("cityId",wxMember.getCityId());
            logger.debug("调用service，获取分类基本信息及分类下的题目列表 参数 mapData:"+mapData);
            Map<String,Object> resultMap = categoryService.findCategoryQuestionList(mapData);
            return AjaxResult.success(resultMap);
        }catch (Exception e){
            logger.error("获取题目失败 "+e.getMessage());
            return AjaxResult.error("操作失败"+e.getMessage());
        }
    }
}
